//
//  SelectTableViewCell.m
//  礼物go
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SelectTableViewCell.h"

static CGFloat const titleH = 30;

@interface SelectTableViewCell ()

@property(nonatomic,assign)BOOL tapStatus;

@end

@implementation SelectTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    self.backImageView = [UIImageView new];
    self.backImageView.backgroundColor = [UIColor purpleColor];
    [self.contentView addSubview:self.backImageView];
    CGFloat y = CGRectGetMaxY(self.backImageView.frame);
    self.blackView = [[UIView alloc] initWithFrame:CGRectMake(8, y - titleH - 30, SCREENWIDTH - 16, titleH + 30)];
    CGFloat a = 1.0;
    for (double i = 0; i < 120; i++) {
        a = (i / 120);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.blackView.frame.size.height - i * 0.5, SCREENWIDTH - 16, 0.5)];
        view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0 - a];
        [self.blackView addSubview:view];
    }
    self.blackView.alpha = 0.5;
    [self.contentView addSubview:self.blackView];
    
    self.titleLabel = [UILabel new];
    self.titleLabel.text = @"标题";
    self.titleLabel.textColor = [UIColor whiteColor];
    //self.titleLabel.backgroundColor = [UIColor blackColor];
    //self.titleLabel.alpha = 0.1;
    [self.contentView addSubview:self.titleLabel];
    
    [self createLikeView];
    
}

-(void)createLikeView{
   self.likeView = [[UIView alloc] initWithFrame:CGRectMake(SCREENWIDTH / 6 * 5, titleH / 2 , SCREENWIDTH / 6 + 10, 20)];
    [self.contentView addSubview:self.likeView];
    self.likeView.userInteractionEnabled = YES;
    self.likeView.layer.masksToBounds = YES;
    self.likeView.layer.cornerRadius = 10;
    self.likeView.alpha = 0.7;
    self.likeView.backgroundColor = [UIColor whiteColor];
    
    //添加点击
    self.tapStatus = NO;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.likeView addGestureRecognizer:tap];
    
    
    self.heartImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.likeView.frame.origin.x + 6, self.likeView.frame.origin.y + 3, self.likeView.frame.size.width / 3 - 11, self.likeView.frame.size.height - 6)];
    //self.heartImageView.backgroundColor = [UIColor blueColor];
   self.heartImageView.image = [UIImage imageNamed:@"heart1"];
//    self.heartImageView.layer.masksToBounds = YES;
//    self.heartImageView.layer.cornerRadius = 10;
    [self.contentView addSubview:self.heartImageView];
    
    self.likeCountsLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.likeView.frame.origin.x + self.likeView.frame.size.width / 3, self.likeView.frame.origin.y, self.likeView.frame.size.width / 3 * 2, self.likeView.frame.size.height)];
    //self.likeCountsLabel.backgroundColor = [UIColor whiteColor];
    self.likeCountsLabel.font = [UIFont systemFontOfSize:12];
    self.likeCountsLabel.adjustsFontSizeToFitWidth = YES;
    //self.likeCountsLabel.text = @"人数";
    [self.contentView addSubview:self.likeCountsLabel];
    
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.backImageView.frame = CGRectMake(8, 10, SCREENWIDTH - 16, self.contentView.frame.size.height - 10);
    self.likeView.frame = CGRectMake(SCREENWIDTH / 6 * 5, titleH / 2 , SCREENWIDTH / 6 + 10, 20);
     CGFloat y = CGRectGetMaxY(self.backImageView.frame);
  
    self.blackView.frame = CGRectMake(8, y - titleH - 30, SCREENWIDTH - 16, titleH + 30);
    //self.blackView = [[UIView alloc] initWithFrame:CGRectMake(8, y - titleH - 30, SCREENWIDTH - 16, titleH + 30)];
//    CGFloat a = 1.0;
//    for (double i = 0; i < 120; i++) {
//        a = (i / 120);
//        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.blackView.frame.size.height - i * 0.5, SCREENWIDTH - 16, 0.5)];
//        view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0 - a];
//        [self.blackView addSubview:view];
//    }
//    self.blackView.alpha = 0.5;
//    [self.contentView addSubview:self.blackView];

    
    self.titleLabel.frame = CGRectMake(13, y - titleH, self.contentView.frame.size.width - 21, titleH);
}

-(void)setSelectContent:(SelectContentModel *)selectContent{
    _selectContent = selectContent;
    [self.backImageView sd_setImageWithURL:[NSURL URLWithString:_selectContent.cover_image_url] placeholderImage:[UIImage imageNamed:@"picholder"]];
    self.titleLabel.text = _selectContent.title;
    NSString *num = [NSString stringWithFormat:@"%@",_selectContent.likes_count];
   // self.likeCountsLabel.text = num;
    NSInteger number = [num integerValue];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *userName = [user objectForKey:@"userName"];
    NSString *string = [user objectForKey:@"status"];
    if ([string isEqualToString:@"yes"]) {
   if ([[DataFMDBHandle shareDataFMDBHandle] presentDataWithId:_selectContent.myId WithAccount:userName]) {
            self.likeCountsLabel.text = [NSString stringWithFormat:@"%ld",number + 1];
             self.heartImageView.image = [UIImage imageNamed:@"heart2"];
             self.selectContent.isSelected = YES;
            return;
        }
         self.likeCountsLabel.text = [NSString stringWithFormat:@"%ld",number];
        self.heartImageView.image = [UIImage imageNamed:@"heart1"];
        self.selectContent.isSelected = NO;
    } else {
    self.likeCountsLabel.text = [NSString stringWithFormat:@"%ld",number];
    
    }
    
}


-(void)tapAction:(UITapGestureRecognizer *)tap{
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    keyAnimation.values = @[@(0.5),@(1.0),@(1.5)];
    keyAnimation.duration = 0.3;
    [self.heartImageView.layer addAnimation:keyAnimation forKey:@"key"];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *userName = [user objectForKey:@"userName"];
    NSString *string = [user objectForKey:@"status"];
    if ([string isEqualToString:@"yes"]) {
        if (self.selectContent.isSelected) {
            //已选择收藏 再次点击 取消收藏
            [[DataFMDBHandle shareDataFMDBHandle] deletePresent:userName MyId:_selectContent.myId];
            
            self.heartImageView.image = [UIImage imageNamed:@"heart1"];
            NSString *num = self.likeCountsLabel.text;
            NSInteger number = [num integerValue] - 1;
            self.likeCountsLabel.text = [NSString stringWithFormat:@"%ld",number];
          
            
        }else{
         //未选择收藏 再次点击 收藏
            [[DataFMDBHandle shareDataFMDBHandle] insertPresentContentModel:_selectContent WithAccount:userName];
            
            self.heartImageView.image = [UIImage imageNamed:@"heart2"];
            NSString *num = self.likeCountsLabel.text;
            NSInteger number = [num integerValue] + 1;
            self.likeCountsLabel.text = [NSString stringWithFormat:@"%ld",number];
        }

        self.selectContent.isSelected = !self.selectContent.isSelected;
        return;
}
      self.likePush();
    
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
